(in-package :com.ozten.knit.client)

(defun schedule-draw-sim (canvas &optional (millis 2000))
  (after millis  #'(lambda () (draw-sim canvas))))

(defun main ()
  (with-ltk ()
    (let* ((canvas (make-instance 'canvas :width 500 :height :500 :master nil)))
      (schedule-draw-sim canvas 1166)
      (pack canvas))))

(defclass k-path ()
  (tk-id))

(defun draw-sim (canvas)
;  (setf *canvas* canvas)
;  (itemconfigure canvas (create-rectangle canvas 0 0 300 300) :fill :white)
  (loop for x downfrom 300 to 0 by 50 do
	(loop for y downfrom 300 to 0 by 55 do
	      (let ((top          (create-polygon canvas (move-by x y (top-loop-shape))))
		    (left-top     (create-polygon canvas (move-by x y (left-loop-shape))))
		    (right-top    (create-polygon canvas (move-by x y (right-loop-shape))))
		    (bottom-left  (create-polygon canvas (move-by x y (left-bottom-loop-shape))))
		    (bottom-right (create-polygon canvas (move-by x y (right-bottom-loop-shape)))))
    ;(format t "~a~%" (cget canvas :height))
    ;(format t "~a~%" (configure canvas :height))
    
		(dolist  (poly (list top left-top right-top bottom-left bottom-right))
		  (if (not (or (eq poly bottom-left)
			  (eq poly bottom-right)))
			   (itemconfigure canvas poly :smooth :bezier))
		  (itemconfigure canvas poly  :fill "#FFFCCCCCCCCC"))))))


    ;(itemdelete canvas "all")
  ;
;  (loop for x from 0 to (1- (length (top-loop-shape) )) by 2
;	     do (let ((x (nth x (top-loop-shape)))
;		      (y (nth (+ x 1) (top-loop-shape))))
;		  (format t "Creating oval at ~d ~d ~d ~d ~%" (- x 2) (- y 2) (+ x 2) (+ y 2))
;		  (create-oval canvas (- x 2) (- y 2) (+ x 2) (+ y 2))))
;
;  
;  
;  (itemconfigure canvas(create-line canvas (bottom-left-loop-shape)) :smooth :bezier)
;  

;  (schedule-draw-sim canvas))


(defun move-by (x-offset y-offset points)
;  "given a list of points 0 0 12 12 move it by x-offset and y-offset"
  (loop for i from 0 to (1- (length points)) 
	     collect (if (evenp i)
			 (+ x-offset (nth i points))
			 (+ y-offset (nth i points)))))
		  
  

#+ccl (ccl:process-run-function "main-proc" 'main)
#-ccl (main)